<template>
	<uni-card title="菜单">
		<uni-grid :column="3" :showBorder="false" :square="true" @change="menu">

			<uni-grid-item index="0">
				<view class="grid-item-box">
					<uni-icons type="list" size="40"></uni-icons>
					<text class="text">联盟数据排行</text>
				</view>
			</uni-grid-item>
			<uni-grid-item index="1">
				<view class="grid-item-box">
					<uni-icons type="list" size="40"></uni-icons>
					<text class="text">联盟数据对比</text>
				</view>
			</uni-grid-item>
			<uni-grid-item index="2">
				<view class="grid-item-box">
					<uni-icons type="list" size="40"></uni-icons>
					<text class="text">联盟DKP</text>
				</view>
			</uni-grid-item>
			<uni-grid-item index="8">
				<view class="grid-item-box">
					<uni-icons type="list" size="40"></uni-icons>
					<text class="text">本区数据排行</text>
				</view>
			</uni-grid-item>
			<uni-grid-item index="9">
				<view class="grid-item-box">
					<uni-icons type="list" size="40"></uni-icons>
					<text class="text">本区数据对比</text>
				</view>
			</uni-grid-item>
			<uni-grid-item index="10">
				<view class="grid-item-box">
					<uni-icons type="list" size="40"></uni-icons>
					<text class="text">本区DKP</text>
				</view>
			</uni-grid-item>
			<uni-grid-item index="3">
				<view class="grid-item-box">
					<uni-icons type="cloud-download" size="40"></uni-icons>
					<text class="text">联盟数据下载</text>
				</view>
			</uni-grid-item>
			<uni-grid-item index="11">
				<view class="grid-item-box">
					<uni-icons type="cloud-download" size="40"></uni-icons>
					<text class="text">本区数据下载</text>
				</view>
			</uni-grid-item>
			<uni-grid-item index="4" v-if="role==1">
				<view class="grid-item-box">
					<uni-icons type="close" size="40"></uni-icons>
					<text class="text">退出联盟</text>
				</view>
			</uni-grid-item>
			<uni-grid-item index="5" v-if="role==2">
				<view class="grid-item-box">
					<uni-icons type="trash" size="40"></uni-icons>
					<text class="text">删除联盟</text>
				</view>
			</uni-grid-item>
			<uni-grid-item index="6" v-if="role==2">
				<view class="grid-item-box">
					<uni-icons type="upload" size="40"></uni-icons>
					<text class="text">数据提取</text>
					<view class="grid-dot" v-if="runningStatus!=''">
						<uni-badge :text="runningStatus" type="primary" />
					</view>
				</view>
			</uni-grid-item>
			<uni-grid-item index="7" v-if="role==2">
				<view class="grid-item-box">
					<uni-icons type="gear" size="40"></uni-icons>
					<text class="text">设置</text>
				</view>
			</uni-grid-item>
		</uni-grid>

		<uni-popup ref="popup" type="dialog">
			<uni-popup-dialog message="提示" :content="dialogContent" :duration="0" @confirm="dialogConfirm">
			</uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="popupQuit" type="dialog">
			<uni-popup-dialog message="提示" :content="dialogContent" :duration="0" @confirm="dialogConfirmQuit">
			</uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="popupDelete" type="dialog">
			<uni-popup-dialog message="提示" :content="dialogContent" :duration="0" @confirm="dialogConfirmDelete">
			</uni-popup-dialog>
		</uni-popup>
		<!-- 提示信息弹窗 -->
		<uni-popup ref="message" type="message" :value="popupMessage">
			<uni-popup-message :type="popupMessage.msgType" :message="popupMessage.messageText" :duration="1000">
			</uni-popup-message>
		</uni-popup>
	</uni-card>
</template>

<script>
	export default {
		data() {
			return {
				unionId: '',
				role: 0,
				dialogContent: '',
				runningStatus: '',
				popupMessage: {
					type: 'center',
					msgType: 'success',
					messageText: '这是一条成功提示',
					value: ''
				}
			}
		},
		onLoad: function(option) {
			this.unionId = option.unionId
			this.onload()
		},
		methods: {
			menu(e) {
				switch (e.detail.index) {
					case 0:
						uni.navigateTo({
							url: "/pages/unionStatRank/unionStatRank?server=0&unionId=" + this.unionId
						})
						break
					case 1:
						uni.navigateTo({
							url: "/pages/unionStatCompare/unionStatCompare?server=0&unionId=" + this.unionId
						})
						break
					case 2:
						uni.navigateTo({
							url: "/pages/unionDKP/unionDKP?server=0&unionId=" + this.unionId
						})
						break
					case 3:
						uni.navigateTo({
							url: "/pages/unionStatDownload/unionStatDownload?server=0&unionId=" + this.unionId
						})
						break
					case 4:
						this.dialogContent = "确定退出联盟吗？"
						this.$refs.popupQuit.open()
						break
					case 5:
						this.dialogContent = "确定删除联盟吗？"
						this.$refs.popupDelete.open()
						break
					case 6:
						if (this.runningStatus == '') {
							this.dialogContent = "提取数据需要扣除10积分，确定执行吗？"
							this.$refs.popup.open()
						}
						break
					case 7:
						uni.navigateTo({
							url: "/pages/updateUnion/updateUnion?unionId=" + this.unionId
						})
						break
					case 8:
						uni.navigateTo({
							url: "/pages/unionStatRank/unionStatRank?server=1&unionId=" + this.unionId
						})
						break
					case 9:
						uni.navigateTo({
							url: "/pages/unionStatCompare/unionStatCompare?server=1&unionId=" + this.unionId
						})
						break
					case 10:
						uni.navigateTo({
							url: "/pages/unionDKP/unionDKP?server=1&unionId=" + this.unionId
						})
						break
					case 11:
						uni.navigateTo({
							url: "/pages/unionStatDownload/unionStatDownload?server=1&unionId=" + this.unionId
						})
						break
				}
			},
			dialogConfirm() {
				uni.request({
					url: getApp().globalData.url + '/api/user/addTask',
					method: "POST",
					header: {
						'content-type': 'application/json',
						'token': getApp().globalData.token
					},
					data: {
						unionId: this.unionId
					},
					success: (res) => {
						if (res.data.code == "00000") {
							this.runningStatus = '提取中'
						} else {
							this.popupMessage.msgType = "error"
							this.popupMessage.messageText = res.data.msg
							this.$refs.message.open()
						}
					}
				});
				
				uni.requestSubscribeMessage({
				  tmplIds: ['QHwd_XCdVJzQC8VDnjPfUSGVc8pkSnRHmiXouenF58o'],
				  complete (res) { 
					  
				  }
				})
				
			},
			dialogConfirmQuit() {
				uni.request({
					url: getApp().globalData.url + '/api/union/quitUnion',
					method: "POST",
					header: {
						'content-type': 'application/json',
						'token': getApp().globalData.token
					},
					data: {
						unionId: this.unionId,
						userId: getApp().globalData.user.id
					},
					success: (res) => {
						if (res.data.code == "00000") {
							this.popupMessage.msgType = "success"
							this.popupMessage.messageText = "成功"
							this.$refs.message.open()
							uni.navigateTo({
								url: "/pages/menu/menu"
							})
						} else {
							this.popupMessage.msgType = "error"
							this.popupMessage.messageText = res.data.msg
							this.$refs.message.open()
						}
					}
				});
			},
			dialogConfirmDelete() {
				uni.request({
					url: getApp().globalData.url + '/api/union/deleteUnion',
					method: "POST",
					header: {
						'content-type': 'application/json',
						'token': getApp().globalData.token
					},
					data: {
						unionId: this.unionId,
						userId: getApp().globalData.user.id
					},
					success: (res) => {
						if (res.data.code == "00000") {
							this.popupMessage.msgType = "success"
							this.popupMessage.messageText = "成功"
							this.$refs.message.open()
							uni.navigateTo({
								url: "/pages/menu/menu"
							})
						} else {
							this.popupMessage.msgType = "error"
							this.popupMessage.messageText = res.data.msg
							this.$refs.message.open()
						}
					}
				});
			},
			onload(){
				uni.request({
					url: getApp().globalData.url + '/api/union/getUnion?id=' + this.unionId,
					header: {
						'content-type': 'application/json',
						'token': getApp().globalData.token
					},
					success: (res) => {
						if (res.data.code == "00000") {
							if (getApp().globalData.user.id == res.data.data.userId) {
								this.role = 2
							} else {
								this.role = 1
							}
						} else {
							this.popupMessage.msgType = "error"
							this.popupMessage.messageText = res.data.msg
							this.$refs.message.open()
						}
					}
				});
				uni.request({
					url: getApp().globalData.url + '/api/common/getTaskStatus?unionId=' + this.unionId,
					header: {
						'content-type': 'application/json',
						'token': getApp().globalData.token
					},
					success: (res) => {
						if (res.data.code == "00000") {
							if (res.data.data != null) {
								if (res.data.data.status == 0) {
									this.runningStatus = '排队中'
								} else if (res.data.data.status == 1) {
									this.runningStatus = '执行中'
								}
							}
						} else {
							this.popupMessage.msgType = "error"
							this.popupMessage.messageText = res.data.msg
							this.$refs.message.open()
						}
				
					}
				});
			}
		},
		onShareAppMessage: function(options) {
			var that = this;
			var shareObj = {
				path: '/pages/menu/menu'
			};
			return shareObj;
		},
		onPullDownRefresh:function(){
			this.onload()
			uni.stopPullDownRefresh()
		}
	}
</script>

<style>
	.grid-item-box {
		flex: 1;
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.grid-dot {
		position: absolute;
		top: 5px;
		right: 15px;
	}

	.container {
		padding: 20 rpx;
	}
</style>
